package com.force.langchain4j.filesource;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.springframework.core.annotation.Order;

import java.io.InputStream;

@Order(3)
public class JSONFileLoader implements FileSourceLoader{

    private static final String suffix = "json";
    @SneakyThrows
    @Override
    public Object getSourceContent(FileBaseLoader file){
        InputStream inputStream = file.getInputStream();
        if (suffix.equals(file.getSuffix())) {
            ObjectMapper mapper = new ObjectMapper();

            // 将 InputStream 解析为通用 Map 或具体类对象
            Object jsonObject = mapper.readValue(inputStream, Object.class);

            // 输出为 JSON 字符串（格式化可选）
            String jsonString = mapper.writeValueAsString(jsonObject);
            return jsonString;
        }
        throw new RuntimeException("未实现的文件解析类型:"+file.getSuffix());
    }


}
